package com.kikcn.demo.services;

import com.kikcn.demo.dao.BookmarkFolderRepository;
import com.kikcn.demo.dao.BookmarkRepository;
import com.kikcn.demo.domain.Bookmark;
import com.kikcn.demo.domain.BookmarkFolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * User: wlai
 * Date: 4/10/15
 * Time: 10:18 AM
 */
@Service
@Transactional
public class BookmarkService {
    @Autowired
    private BookmarkFolderRepository folderRepository;
    @Autowired
    private BookmarkRepository bookmarkRepository;

    public Page<BookmarkFolder> listFolder(Pageable pageable){
        return folderRepository.findAll(pageable);
    }

    public void saveFolder(BookmarkFolder bookmarkFolder){
        folderRepository.save(bookmarkFolder);
    }

    public void deleteFolder(BookmarkFolder bookmarkFolder){
        folderRepository.delete(bookmarkFolder);
    }
    public Page<Bookmark> listBookmark(Pageable pageable){

        return bookmarkRepository.findAll(pageable);
    }

    public void saveBookmark(Bookmark bookmark ){
        bookmarkRepository.save(bookmark);
    }

    public void deleteBookmark(Bookmark bookmark){
        bookmarkRepository.delete(bookmark);
    }

    public List<Bookmark> findByBookmarkFolder(BookmarkFolder folder){
        return bookmarkRepository.findByParent(folder);
    }

    public List<BookmarkFolder> findFolderByBookmarkFolder(BookmarkFolder folder){
        return folderRepository.findByParent(folder);
    }

    public BookmarkFolder findFolderById(Integer folderId) {
        return folderRepository.findOne(folderId);
    }

    public Bookmark findBookmarkById(Integer bId) {
        return bookmarkRepository.findOne(bId);
    }
}
